package fr.umlv.galcon.planet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.Random;

import fr.umlv.galcon.player.PlayerType;

public class StockManager {
    private final Planet planet;
    private final int productionRate;
    private int producedShip;
    private int shipStock;
    private int realStock;

    /**
     * Planet's Stock Manager constructor
     * 
     * @param planet
     *            : The Manager's planet
     * @param productionRate
     *            : Speed production of the planet
     * @param shipStock
     *            : Quantity of Ships available at the creation of the Planet.
     * @param realStock
     *            : The real stock of the planet
     */
    public StockManager(Planet planet, int productionRate, int shipStock, int realStock) {
	this.planet = planet;
	this.shipStock = shipStock;
	this.productionRate = new Random().nextInt(5) + 1;
	this.producedShip = 0;
	this.realStock = realStock;
    }

    /**
     * Display the stock of the Planet.
     * 
     * @param g
     *            The graphical renderer.
     */
    public void displayStock(Graphics2D g) {
	if (this.planet.getOwner().getPlayerType() != PlayerType.COMPUTER) {
	    g.setColor(Color.BLACK);
	    g.setFont(new Font("Arial", Font.BOLD, 15));
	    g.drawString(Integer.toString(this.shipStock), (int) this.planet.getCenter().getX() - 5, (int) this.planet.getCenter()
		    .getY() + 8);
	}
    }

    /**
     * Get the Planet's stock production rate
     * 
     * @return the production rate
     */
    public int getProductionRate() {
	return this.productionRate;
    }

    /**
     * Get the Planet's real stock
     * 
     * @return the real ship stock
     */
    public int getRealStock() {
	return this.realStock;
    }

    /**
     * Get the Planet's ships stock
     * 
     * @return nb of ship into the stock
     */
    public int getShipStock() {
	return this.shipStock;
    }

    /**
     * Produce ships.
     */
    public void produce() {
	this.producedShip++;
	if (this.producedShip >= this.productionRate * this.planet.getShipProperties().getProduction()) {
	    this.shipStock++;
	    this.producedShip = 0;
	    this.realStock++;
	}
    }

    /**
     * Raise the Ship stock
     */
    public void raiseStock() {
	this.shipStock++;
	if (this.shipStock > 0)
	    this.realStock++;
    }

    /**
     * Reduce the Ship stock
     */
    public void reduceStock(int impactStrength) {
	this.shipStock -= impactStrength;
    }

    /**
     * Set the realStock of the Planet.
     * 
     * @param realStock
     *            : The new real stock.
     */
    public void setRealStock(int realStock) {
	this.realStock = realStock;
    }

    /**
     * Set the ship stock of the Planet.
     * 
     * @param shipStock
     *            The new Ship stock.
     */
    public void setShipStock(int shipStock) {
	this.shipStock = shipStock;
    }

}
